package com.mimaxueyuan.disruptor.demo1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

/**
 * 使用Translator接口,替代Producer,简化代码,降低出错几率
 * @author: Kevin
 * @官网: 	www.mimaxueyuan.com
 * @Q Q群:	660567408
 * @Email:	mimaxueyuan@163.com
 * [每天进步一点点、人生带来大改变...]
 * [本代码对应视频地址:http://study.163.com/course/introduction/1004573050.htm]
 */
public class LongEventMain2 {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws InterruptedException {
		ExecutorService executor = Executors.newCachedThreadPool();
		LongEventFactory factory = new LongEventFactory();
		int bufferSize = 1024;
		Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize, executor);
		disruptor.handleEventsWith(new LongEventHandler());
		disruptor.start();
		RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
		
		LongEventProducerWithTranslator producer = new LongEventProducerWithTranslator(ringBuffer);
		
		//producer.publish(100L, "张三");
		
		producer.onData();
		Thread.sleep(1000);
		
		producer.onData(1L);
		Thread.sleep(1000);
		
		producer.onData(1L,2L);
		Thread.sleep(1000);
		
		producer.onData(1,2L,3L);
		Thread.sleep(1000);
		
		producer.onData(1L,2L,"3","4","5");
		Thread.sleep(1000);
		
		//关闭线程池
		executor.shutdown();
		//关闭Disruptor
		disruptor.shutdown();
	}

}
